package com.li.core.config.rebbitmq.demo;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Description：
 * @Author：LiDong
 * @Create：2021/1/13
 * @Version：1.0.0
 */
@RestController
@RequestMapping("/rabbit")
public class OsCoreRabbitMqProduct {

    /**
     * 队列名
     */
    private static final String LOG_QUEUE = "log_queue";

    /**
     * 交换机名
     */
    public static final String LOG_EXCHANGE = "log_exchange";

    public static final String DIRECT_ROUTINGKEY = "test";


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/test1")
    public void sendMessage() {
        String context = "simple---> " + new Date();
        //如果没有配置默认交换机,直接传入queue的name
        rabbitTemplate.convertAndSend(LOG_QUEUE, context);
        //如果配置了默认的交换机，（交换机，queue_name，内容）
        rabbitTemplate.convertAndSend("", DIRECT_ROUTINGKEY, context);
    }

    @RequestMapping("/test2")
    public void sendMessages() {
        String context = "direct---> " + new Date();
        //（交换机名称,路由的key,内容）
        rabbitTemplate.convertAndSend(LOG_EXCHANGE, DIRECT_ROUTINGKEY, context);
    }


}
